package com.zyg.apart.controller;

import com.github.pagehelper.PageInfo;
import com.zyg.apart.model.Flats;
import com.zyg.apart.model.Leasecontract;
import com.zyg.apart.model.Lessee;
import com.zyg.apart.service.FlatsService;
import com.zyg.apart.service.LeasecontractService;
import com.zyg.apart.service.LesseeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/contract")
public class LeasecontractController {
    @Autowired
    private LeasecontractService leasecontractService;
    @Autowired
    private FlatsService flatsService;
    @Autowired
    private LesseeService lesseeService;


    @RequestMapping("/list")
    public String list(Integer currentPage, Model model){
        if (currentPage == null){
            currentPage = 1;
        }
        PageInfo<Map<String,Object>> lcs = leasecontractService.getAllLC(currentPage);
        model.addAttribute("lcs",lcs);
        return "contract/list";
    }


    @RequestMapping("/detail")
    public String detail(Model model,Integer id){
        Map<String,Object> lc = leasecontractService.getLCById(id);
        model.addAttribute("lc",lc);
        return "contract/detail";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Model model, Integer id){
        Map<String,Object> lc = leasecontractService.getLCById(id);
        model.addAttribute("lc",lc);
        return "contract/edit";
    }

    @RequestMapping("/edit")
    public String edit(Leasecontract leasecontract){
        int i = leasecontractService.editLC(leasecontract);

        return "forward:list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Lessee> lss = lesseeService.getLSNames();
        List<Flats> flats = flatsService.getAllName();
        model.addAttribute("lss",lss);
        model.addAttribute("flats",flats);
        return "contract/add";
    }

    @RequestMapping("/add")
    public String add(Leasecontract leasecontract){
        int i = leasecontractService.addLC(leasecontract);
        return "forward:list";
    }

    @RequestMapping("/del")
    public String del(Integer id){
        int i = leasecontractService.delLCById(id);
        return "forward:list";
    }




}
